var express = require('express');
var router = express.Router();
var User = require('../db/user.js');
var dynamic = require('../db/dynamic');
const fs = require('fs');


//评论
router.post("/doCommit", function (req, res, next) {
    let did = req.body.did;
    let comment = req.body.comment;
    let addTime = req.body.addTime;
    User.findOne({ uid: req.cookies.userID }, function (err, data) {
        if (err) { console.log(err); return; }
        dynamic.updateOne({ d_id: did }, { $addToSet: { comment: { c_id: "dyCo/" + req.cookies.userID + "/" + addTime, c_name: data.uname, c_imgHead: data.img_head, c_addTime: addTime, c_content: comment, c_type: false, } } }, function (err) {
            if (err) { console.log(err); return; }
            else {
                res.send("评论成功！")
            }
        });
    })
});

//收藏
router.post("/doCollection", function (req, res, next) {
    dynamic.findOne({ d_id: req.body.did }, function (err, list) {
        if (err) { console.log(err); return; }
        if (list.collectionCount.length != 0) {
            for (let i = 0; i < list.collectionCount.length; i++) {
                if (req.cookies.userID == list.collectionCount[i]) {
                    res.send("你已经收藏过了哦！");
                    return;
                } else { }
            }
        }
        dynamic.updateOne({ d_id: req.body.did }, { $addToSet: { collectionCount: req.cookies.userID } }, function (err) {
            if (err) {
                console.log(err);
                return;
            } else {
                User.updateOne({ uid: req.cookies.userID }, { $addToSet: { likes: req.body.did } }, function (err) {
                    if (err) { console.log(err); return; }
                    res.send("收藏成功！");
                })
            }
        });
    })
});

//Happyroot主页文章列表获取
router.post("/doHappyRoot", function (req, res, next) {
    dynamic.find({ d_type: 1 }, function (err, data) {
        if (err) { console.log(err); return; }
        if (data.length != 0 && data.length < 30) {
            async function getDynamicList() {
                let alldynamic = [];
                for (let i = data.length - 1; i >= 0; i--) {
                    await User.findOne({ uid: data[i].uid }, function (err, item) {
                        if (err) { console.log(err); return; }
                        // return item.img_head;
                        let text = data[i].content.replace(new RegExp("\n", 'g'), "<br>");
                        alldynamic.push({
                            did: data[i].d_id,
                            uname: data[i].uname,
                            addTime: data[i].showTime,
                            imgHead: item.img_head,
                            content: text,
                            likeNum: data[i].likeCount.length,
                            collectionNum: data[i].collectionCount.length,
                            commentNum: data[i].comment.length,
                            picList: data[i].pictureUrl,
                        });
                    })
                }
                return alldynamic;
            }
            getDynamicList().then(result => {
                let allDynamic = result;
                // console.log(allDynamic);
                res.send(allDynamic);
            })
        } else if (data.length >= 30) {
            async function getDynamicList() {
                let alldynamic = [];
                for (let i = data.length - 1; i >= data.length - 30; i--) {
                    await User.findOne({ uid: data[i].uid }, function (err, item) {
                        if (err) { console.log(err); return; }
                        // return item.img_head;
                        let text = data[i].content.replace(new RegExp("\n", 'g'), "<br>");
                        alldynamic.push({
                            did: data[i].d_id,
                            uname: data[i].uname,
                            addTime: data[i].showTime,
                            imgHead: item.img_head,
                            content: text,
                            likeNum: data[i].likeCount.length,
                            collectionNum: data[i].collectionCount.length,
                            commentNum: data[i].comment.length,
                            picList: data[i].pictureUrl,
                        });
                    })
                }
                return alldynamic;
            }
            getDynamicList().then(result => {
                let allDynamic = result;
                // console.log(allDynamic);
                res.send(allDynamic);
            });
        } else {
            res.send("");
        }
    })
});

//点赞
router.post("/doLike", function (req, res, next) {
    dynamic.findOne({ d_id: req.body.did }, function (err, list) {
        if (err) { console.log(err); return; }
        if (list.likeCount.length != 0) {
            for (let i = 0; i < list.likeCount.length; i++) {
                if (req.cookies.userID == list.likeCount[i]) {
                    res.send("你已经点过赞了哦！");
                    return;
                } else { }
            }
        }
        dynamic.updateOne({ d_id: req.body.did }, { $addToSet: { likeCount: req.cookies.userID } }, function (err) {
            if (err) {
                console.log(err);
                return;
            } else {
                User.updateOne({ uid: list.uid }, { $inc: { numOfLikes: 1 } }, function (err) {
                    if (err) { console.log(err); return; }
                    res.send("点赞成功！");
                })
            }
        });
    })
});

module.exports = router;